<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/5 0005
 * Time: 下午 13:52
 */
namespace app\admin\controller;

use \think\Db;
use \think\Request;
use app\admin\model\Admin as adminModel;//管理员模型
class Convert extends Permissions
{

    /**
     * 旗力币兑换房卡
     * @return mixed
     */
    public function index()
    {
        $list = Db::name('convert')->order('id asc')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }


    /**
    * Created by PhpStorm.
    * User: Administrator
    * Date: 2018/6/5 0005
    *修改套餐
    */
    public function update()
    {
        if($this->request->isPost()) {
            $post = $this->request->post();
            $list = Db::name('convert')->where('id',$post['id'])->update($post);
            if($list) {
                $convert = Db::name('convert')->order('id asc')->select();
                write_static_cache('convert',$convert);
                return $this->success('修改成功','admin/convert/index');
            } else {
                return $this->error('修改失败');
            }
        }else{
            $id = input('id');
            if(!$id){
                return $this->error('参数出错');
            }
            $lis = Db::name('convert')->where('id',$id)->find();
            $this->assign('lis',$lis);
            $this->assign('id',$id);
        }

        return $this->fetch();
    }

    /**
     * 添加兑换套餐
     * @return mixed|void
     * @throws \think\Exception
     */
    public function add()
    {
        if(Request::instance()->isPost()){
            $coin = input('post.coin_one/a');
            $id = input('post.id/a');
            $card = input('post.card_one/a');
            Db::name('convert')->where('id','>',0)->delete();
            for($i=0;$i<count($coin);$i++){
                $list_img['coin_one']= $coin[$i];
                $list_img['card_one']= $card[$i];
                $list_img['id']= $id[$i];
                $list =  Db::name('convert')->insert($list_img);
            }
            if($list){
                $convert = Db::name('convert')->order('id asc')->select();
                write_static_cache('convert',$convert);
                return $this->success('添加成功','admin/convert/index');
            }else{
                return $this->error('添加失败');
            }

        }else{
            $list = Db::name('convert')->order('id asc')->select();
            $this->assign('list',$list);
        }
        return $this->fetch();
    }

    /**
     * 删除兑换
     * @throws \think\Exception
     */
    public function del()
    {
        $id = input('id');
        if(!$id){
            return $this->error('参数出错');
        }
        $lis = Db::name('convert')->where('id',$id)->delete();
        if($lis){
            $convert = Db::name('convert')->order('id asc')->select();
            write_static_cache('convert',$convert);
            return $this->success('删除成功','admin/convert/index');
        }else{
            return $this->error('删除失败');
        }
    }

    /**
     * 充值套餐
     * @return mixed
     */
    public function charges()
    {
        $list = Db::name('price')->order('id asc')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function adds()
    {
        if(Request::instance()->isPost()){
            $coin = input('post.num/a');
            $id = input('post.id/a');
            $card = input('post.price/a');
            $present = input('post.present/a');
            $remark = input('post.remark/a');
            Db::name('price')->where('id','>',0)->delete();
            for($i=0;$i<count($coin);$i++){
                $list_img['num']= $coin[$i];
                $list_img['price']= $card[$i];
                $list_img['present']= $present[$i];
                $list_img['remark']= $remark[$i];
                $list_img['id']= $id[$i];
                $list =  Db::name('price')->insert($list_img);
            }
            if($list){
                $convert = Db::name('price')->order('id asc')->select();
                write_static_cache('price',$convert);
                return $this->success('添加成功','admin/convert/charges');
            }else{
                return $this->error('添加失败');
            }
        }else{
            $list = Db::name('price')->order('id asc')->select();
            $this->assign('list',$list);
        }
        return $this->fetch();
    }

    public function reupdate()
    {
        if($this->request->isPost()) {
            $post = $this->request->post();
            $list = Db::name('price')->where('id',$post['id'])->update($post);
            if($list) {
                $convert = Db::name('price')->order('id asc')->select();
                write_static_cache('price',$convert);
                return $this->success('修改成功','admin/convert/charges');
            } else {
                return $this->error('修改失败');
            }
        }else{
            $id = input('id');
            if(!$id){
                return $this->error('参数出错');
            }
            $lis = Db::name('price')->where('id',$id)->find();
            $this->assign('lis',$lis);
            $this->assign('id',$id);
        }
        return $this->fetch();
    }

    public function detele()
    {
        $id = input('id');
        if(!$id){
            return $this->error('参数出错');
        }
        $lis = Db::name('price')->where('id',$id)->delete();
        if($lis){
            $convert = Db::name('price')->order('id asc')->select();
            write_static_cache('price',$convert);
            return $this->success('删除成功','admin/convert/charges');
        }else{
            return $this->error('删除失败');
        }
    }

   /**
   * Created by PhpStorm.
   * User: Administrator
   *卡包
   */
    public function bag()
    {
        $list = Db::name('bag')->order('id asc')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
   * Created by PhpStorm.
   * User: Administrator
   * Date: 2018/6/5 0005
   *修改卡包
   */
    public function update_bag()
    {
        if($this->request->isPost()) {
            $post = $this->request->post();
            $list = Db::name('bag')->where('id',$post['id'])->update($post);
            if($list) {
                $convert = Db::name('bag')->order('id asc')->select();
                write_static_cache('bag',$convert);
                return $this->success('修改成功','admin/convert/bag');
            } else {
                return $this->error('修改失败');
            }
        }else{
            $id = input('id');
            if(!$id){
                return $this->error('参数出错');
            }
            $lis = Db::name('bag')->where('id',$id)->find();
            $this->assign('lis',$lis);
            $this->assign('id',$id);
        }
        return $this->fetch();
    }

}
